AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:Initialize()
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetCollisionGroup(COLLISION_GROUP_PROJECTILE)
	self.Entity:DrawShadow(false)
	self.Entity:SetGravity(0)
	
	self.Entity.Clear = false
	self.Entity.ClearTime = CurTime()
	
	self.Entity.Exists = true

	local physobj = self.Entity:GetPhysicsObject()
	physobj:SetDamping(0, 0)
	physobj:SetMass(1)
	physobj:EnableGravity(false)
	physobj:EnableDrag(false)
	
	self.Entity.CDamage = self.Entity.CDamage or {}
	self.Entity.CDamage.type = "missile"
	self.Entity.CDamage.maxhealth = 3
	self.Entity.CDamage.health = 3
	
	if (!(self.Entity.IsSubmunition or false)) then
		local netcolorvector = Vector(self.Entity.Color.r, self.Entity.Color.g, self.Entity.Color.b)
		self.Entity:SetNetworkedVector("color", netcolorvector)
		self.Entity:SetNetworkedString("damagetype", self.Entity.DamageType)
	end
	
	self.Entity:SetNetworkedBool("Drawmodel", true)
	self:StartMotionController()
	self.Entity:NextThink(CurTime() + 0.1)
end

function ENT:PhysicsCollide(data, physobj)
	self.Entity:GetPhysicsObject():SetVelocity(Vector(0,0,0))
	if util.tobool(self.Entity.BlastRadius) then
		timer.Simple(0.01, CDamage_blastdamage, self.Entity:GetPos(), self.Entity.BlastRadius, self.Entity.Damage, nil, self.Entity.DamageType)
	end
	timer.Simple(0.01, CDamage_damageent, data.HitEntity, self.Entity.Damage, nil, self.Entity.DamageType)
	self.Entity:DrawImpact(data.HitEntity, -1 * data.HitNormal)
	self.Entity:Desolidify()
end

function ENT:ImpactShield(shield)
	if (self.Entity.Damage > shield:GetHP()) then
		self.Entity.Damage = self.Entity.Damage - shield:GetHP()
		CDamage_damageent(shield, shield:GetHP(), nil, self.Entity.DamageType)
		self.Entity:DrawImpact(shield, CDamage_shieldsurfacenormal(shield, self.Entity:GetPos()))
	else
		CDamage_damageent(shield, self.Entity.Damage, nil, self.Entity.DamageType)
		self.Entity:DrawImpact(shield, CDamage_shieldsurfacenormal(shield, self.Entity:GetPos()))
		self.Entity:Desolidify()
	end
end

function ENT:Think()
	if !util.tobool(self.Entity:WaterLevel()) and !self.Entity.Clear then
		self.Entity.Clear = true
		self.Entity.Cleartime = CurTime()
	end
	if self.Entity.Clear then
		self.Entity.speed = math.Clamp(((CurTime() - self.ClearTime) / self.Entity.acceltime) * self.Entity.maxspeed, self.Entity.minspeed, self.Entity.maxspeed)
	end
	self.Entity:NextThink(CurTime() + 0.1)
	return true
end

function ENT:PhysicsSimulate(phys, deltatime)
	local velocitymove = self.Entity:GetPos() + phys:GetVelocity()
	local resangle, damping
	
	if ((self.Entity.Target != nil) and (self.Entity.Target:IsValid()) and (self.speed > (self.Entity.maxspeed / self.Entity.acceltime))) then
		resangle = (self.Entity.Target:GetPos() - self.Entity:GetPos()):Angle()
		if (self.Entity.Target:GetPos() - self.Entity:GetPos()):LengthSqr() <= 250000 then
			damping = 0
		else
			damping = 5000
		end
	else
		resangle = self.Entity:GetForward():Angle()
		damping = 5000
	end
	
 	self.ShadowParams = {}  	
	self.ShadowParams.secondstoarrive = 1
	self.ShadowParams.pos = self.Entity:GetPos() + (self.Entity:GetForward() * self.Entity.speed / 6.66) --JUST. DONT. ASK.
	self.ShadowParams.angle = resangle
	self.ShadowParams.maxangular = 5000
	self.ShadowParams.maxangulardamp = damping
	self.ShadowParams.maxspeed = self.Entity.speed
	self.ShadowParams.maxspeeddamp = self.Entity.speed
	self.ShadowParams.dampfactor = 0.15
	self.ShadowParams.teleportdistance = 0
	self.ShadowParams.deltatime = deltatime
	
	phys:ComputeShadowControl(self.ShadowParams)
end

function ENT:PhysicsUpdate(phys)
	if util.tobool(self.Entity:WaterLevel()) and (self.Entity.speed > (self.Entity.maxspeed / self.Entity.acceltime)) and self.Entity.Exists then
		self.Entity.Exists = false
		self.Entity:DrawImpact(nil, Vector(0, 0, 1))
		self.Entity:Desolidify()
	end
end

function ENT:Desolidify()
	self.Entity.Exists = false
	self.Entity.speed = 0
	self.Entity:SetMoveType(MOVETYPE_NONE)
	self.Entity:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
	self.Entity:SetNotSolid(true)
	self.Entity:SetNetworkedBool("Drawmodel", false)
	self.Entity.CDamage.type = "immune"
	timer.Simple(3, function(entity) if entity and entity:IsValid() then entity:Remove() end end, self.Entity)
end